package com.ruoyi.cloudcourse.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cloudcourse.domain.FeatherChapterFile;
import com.ruoyi.cloudcourse.service.IFeatherChapterFileService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpSession;

/**
 * 章节文件Controller
 * 
 * @author ruoyi
 * @date 2021-07-14
 */
@Controller
@RequestMapping("/cloudcourse/file")
public class FeatherChapterFileController extends BaseController
{
    private String prefix = "cloudcourse/file";

    @Autowired
    private IFeatherChapterFileService featherChapterFileService;

    @RequiresPermissions("cloudcourse:file:view")
    @GetMapping()
    public String file(Long chapterId, HttpSession session)
    {
        System.err.println(chapterId);
        session.setAttribute("chapterId",chapterId);
        return prefix + "/file";
    }

    /**
     * 查询章节文件列表
     */
    @RequiresPermissions("cloudcourse:file:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FeatherChapterFile featherChapterFile,HttpSession session)
    {
        startPage();
        Long chapterId = (Long)session.getAttribute("chapterId");
        featherChapterFile.setChapterId(chapterId);
        List<FeatherChapterFile> list = featherChapterFileService.selectFeatherChapterFileList(featherChapterFile);
        return getDataTable(list);
    }

    /**
     * 导出章节文件列表
     */
    @RequiresPermissions("cloudcourse:file:export")
    @Log(title = "章节文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FeatherChapterFile featherChapterFile)
    {
        List<FeatherChapterFile> list = featherChapterFileService.selectFeatherChapterFileList(featherChapterFile);
        ExcelUtil<FeatherChapterFile> util = new ExcelUtil<FeatherChapterFile>(FeatherChapterFile.class);
        return util.exportExcel(list, "章节文件数据");
    }

    /**
     * 新增章节文件
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存章节文件
     */
    @RequiresPermissions("cloudcourse:file:add")
    @Log(title = "章节文件", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FeatherChapterFile featherChapterFile)
    {
        return toAjax(featherChapterFileService.insertFeatherChapterFile(featherChapterFile));
    }

    /**
     * 修改章节文件
     */
    @GetMapping("/edit/{fileId}")
    public String edit(@PathVariable("fileId") Long fileId, ModelMap mmap)
    {
        FeatherChapterFile featherChapterFile = featherChapterFileService.selectFeatherChapterFileById(fileId);
        mmap.put("featherChapterFile", featherChapterFile);
        return prefix + "/edit";
    }

    /**
     * 修改保存章节文件
     */
    @RequiresPermissions("cloudcourse:file:edit")
    @Log(title = "章节文件", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FeatherChapterFile featherChapterFile)
    {
        return toAjax(featherChapterFileService.updateFeatherChapterFile(featherChapterFile));
    }

    /**
     * 删除章节文件
     */
    @RequiresPermissions("cloudcourse:file:remove")
    @Log(title = "章节文件", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(featherChapterFileService.deleteFeatherChapterFileByIds(ids));
    }
}
